package x.trident.core.configuration;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import x.trident.core.constants.DateFormatConstant;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * wuqiangfu special annotation
 *
 * @Package:        com.maas.platform.configure.json
 * @FileName:       JacksonConfiguration.java
 * @ClassName:      JacksonConfiguration
 * @Description:    jackson配置
 * @Author:         wuqiangfu
 * @CreateDate:     2022/6/10 14:59
 * @UpdateUser:     wuqiangfu
 * @UpdateDate:     2022/6/10 14:59
 * @UpdateRemark:   说明本次修改内容
 * @Version:        v1.0
 */
@Configuration
public class JacksonConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer defaultJackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(Long.class, ToStringSerializer.instance)
                .serializationInclusion(JsonInclude.Include.ALWAYS)
                .dateFormat(new SimpleDateFormat(DateFormatConstant.DEFAULT_DATE_TIME_FORMAT))
                .serializerByType(LocalDateTime.class,
                        new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateFormatConstant.DEFAULT_DATE_TIME_FORMAT)))
                .deserializerByType(LocalDateTime.class,
                        new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateFormatConstant.DEFAULT_DATE_TIME_FORMAT)))
                .serializerByType(LocalDate.class,
                        new LocalDateSerializer(DateTimeFormatter.ofPattern(DateFormatConstant.DEFAULT_DATE_FORMAT)))
                .deserializerByType(LocalDate.class,
                        new LocalDateDeserializer(DateTimeFormatter.ofPattern(DateFormatConstant.DEFAULT_DATE_FORMAT)));
    }

}
